package com.zl.learning.io.file;

import com.zl.learning.io.file.model.AlbumTarget;
import com.zl.learning.io.file.model.AlbumTargetHotspot;
import com.zl.learning.io.file.model.AlbumTargetPhoto;
import org.junit.Test;
import org.zeroturnaround.zip.ZipUtil;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.stream.Collectors;

public class TestFile {

    @Test
    public void test1() throws IOException {
        String baseDir = "批量添加全息目标模板";
        Files.walkFileTree(Paths.get("D:\\data\\data", baseDir), new SimpleFileVisitor<Path>() {
            AlbumTarget albumTarget = null;
            String currentRoot = "";
            String currentDir = "";

            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                String fileName = dir.getFileName().toString();
                System.out.println(fileName);
                if (albumTarget == null) {
                    albumTarget = new AlbumTarget();
                    String filename = dir.getFileName().toString();
                    String[] names = filename.split("_");
                    albumTarget.setName(names[0]);
                    albumTarget.setNativePlace(names[1]);
                    albumTarget.setNation(names[2]);
                    currentRoot = dir.getFileName().toString();
                } else {
                    currentDir = dir.getFileName().toString();
                }
                return super.preVisitDirectory(dir, attrs);
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                String fileName = file.getFileName().toString();
//                System.out.println(file.getFileName());
                return super.visitFile(file, attrs);
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                String fileName = dir.getFileName().toString();
                if (fileName.equals(currentRoot)) {
                    albumTarget = null;
                    currentRoot = null;
                }
                if (fileName.equals(currentDir)) {
                    currentDir = null;
                }
//                Files.delete(dir);
                return super.postVisitDirectory(dir, exc);
            }
        });
    }

    @Test
    public void test2() throws IOException {
        File zipFile = new File("D:\\data\\data\\批量添加全息目标模板.zip");
        File file = new File("D:\\data\\data\\批量添加全息目标模板");
//        ZipUtil.unpack(zipFile, file, Charset.forName("GBK"));
//        Files.walkFileTree(Paths.get(file.getAbsolutePath()), EnumSet.noneOf(FileVisitOption.class),2,new SimpleFileVisitor<Path>(){
//            @Override
//            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
//                System.out.println(dir.getFileName());
//                return super.preVisitDirectory(dir, attrs);
//            }
//
//            @Override
//            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
//                return super.postVisitDirectory(dir, exc);
//            }
//        });
        String[] files = file.list();
        List<AlbumTarget> albumTargets = new ArrayList<>();
        assert files != null;
        for (String f : files) {
            AlbumTarget target = new AlbumTarget();
            String[] names = f.split("_");
            target.setName(names[0]);
            target.setNation(names[2]);
            parseDir(f, target);
            albumTargets.add(target);
        }
        file.delete();
        System.out.println(albumTargets);
    }

    private void parseDir(String file, AlbumTarget albumTarget) throws IOException {
        String baseDir = "批量添加全息目标模板";
        Files.walkFileTree(Paths.get("D:\\data\\data", baseDir, file), new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                if (file.toString().contains("人脸")) {
                    List<AlbumTargetPhoto> photoList = albumTarget.getPhotoList();
                    if (photoList == null) photoList = new ArrayList<>();
                    albumTarget.setPhotoList(photoList);
                    AlbumTargetPhoto photo = new AlbumTargetPhoto();
                    photo.setRemark(file.getFileName().toString());
                }
                if (file.getFileName().toString().equals("imsi.txt")) {
                    List<String> imsis = Files.readAllLines(file);
                    List<AlbumTargetHotspot> hotspotList = imsis.stream().map(AlbumTargetHotspot::new).collect(Collectors.toList());
                    albumTarget.setHotspotList(hotspotList);
                }
                Files.delete(file);
                return super.visitFile(file, attrs);
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                Files.delete(dir);
                return super.postVisitDirectory(dir, exc);
            }
        });
    }
}
